package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import dao.DiaryDAO;
import po.Diary;

/**
* @author 曹桢瑞
* DiaryDAO实现
*/
public class DiaryDAOImpl implements DiaryDAO {
	private Connection con=null;
	public DiaryDAOImpl(Connection con) {
		this.con=con;
	}

	@Override
	public ArrayList<Diary> getDiary(String userId, long submitTime) {
		String sql="select * from darkcirclenovel.log where userId='"+userId+"' and timeStamp="+submitTime;
		ArrayList<Diary> list=new ArrayList<Diary>();
		try {
			PreparedStatement pstmt=con.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()) {
				String content=rs.getString("content");
				Diary diary=new Diary(userId,content,submitTime);
				list.add(diary);
			}
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return list;
	}

	@Override
	public ArrayList<Diary> getDiaryList(String userId, int num) {
		String sql="select * from darkcirclenovel.log where userId='"+userId+"' order by timeStamp desc limit "+num;
		ArrayList<Diary> list=new ArrayList<Diary>();
		try {
			PreparedStatement pstmt=con.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()) {
				String content=rs.getString("content");
				long submitTime=rs.getLong("timeStamp");
				Diary diary=new Diary(userId,content,submitTime);
				list.add(diary);
			}
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return list;
	}

	@Override
	public boolean addDiary(Diary diary) {
		int num = 0;
	    String sql = "insert into diary (userId,content,timeStamp) values(?,?,?)";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) con.prepareStatement(sql);
	        pstmt.setString(1, diary.getUserId());
	        pstmt.setString(2, diary.getContent());
	        pstmt.setLong(3, diary.getSubmitTime());
	        num = pstmt.executeUpdate();
	        pstmt.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    if(num==1) {
	    	return true;
	    }
	    else {
	    	return false;
	    }
	}

	@Override
	public boolean removeDiary(String userId, long submitTime) {
		int num = 0;
	    String sql = "delete from darkcirclenovel.log where userId='"+userId+"' and timeStamp="+submitTime;
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) con.prepareStatement(sql);
	        num = pstmt.executeUpdate();
	        pstmt.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    if(num==1) {
	    	return true;
	    }
	    else {
	    	return false;
	    }
	}

	@Override
	public boolean updateDiary(Diary diary) {
		int num = 0;
	    String sql="update darkcirclenovel.log set content='"+diary.getContent()+"' where userId='"+diary.getUserId()+"' and timeStamp="+diary.getSubmitTime();
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) con.prepareStatement(sql);
	        num = pstmt.executeUpdate();
	        pstmt.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    if(num==1) {
	    	return true;
	    }
	    else {
	    	return false;
	    }
	}
}
